package com.opages.mvc.adapter.consul.utils;

import com.ecwid.consul.v1.health.model.HealthService;
import com.opages.mvc.adapter.common.utils.IPUtils;
import org.springframework.util.StringUtils;

/**
 * @author daizhong.liu
 **/
public class ConsulServiceUtils {
    /**
     * 根据consul心跳服务查找host
     **/
    public static String findHostByHealth(HealthService healthService) {
        HealthService.Service service = healthService.getService();
        HealthService.Node node = healthService.getNode();

        if (StringUtils.hasText(service.getAddress())) {
            return IPUtils.fixIPv6Address(service.getAddress());
        }
        else if (StringUtils.hasText(node.getAddress())) {
            return IPUtils.fixIPv6Address(node.getAddress());
        }
        return node.getNode();
    }
}
